package com.enniswu.weitter.data.remote;

import android.support.annotation.Nullable;

import com.enniswu.weitter.base.BaseModel;
import com.enniswu.weitter.data.local.SPSupport;
import com.enniswu.weitter.entity.JsonBean;
import com.enniswu.weitter.entity.User;
import com.enniswu.weitter.network.HttpClient;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.enniswu.weitter.entity.JsonBean.TypeStore.intListType;
import static com.enniswu.weitter.entity.JsonBean.TypeStore.objType;
import static com.enniswu.weitter.entity.JsonBean.TypeStore.userType;

public class UserLoader extends BaseModel {

    public void getInfo(int id, final FileLoader.ImageCallback callback) {
        Map<String, String> params = new HashMap<>(1, 1);
        params.put("id", String.valueOf(id));
        HttpClient.getInstance().post(null,
                HttpClient.URLStore.USER_INFO_GET, params, new HttpCallbackImpl<User>(callback, userType) {
                    @Override
                    public void onSuccess(String json) {
                        JsonBean<User> jsonBean = new Gson().fromJson(json, userType);
                        User user = jsonBean.getT();
                        if (user == null) {
                            callback.onFailure(HttpClient.getErrorMessage());
                            return;
                        }
                        callback.onSuccess(user);
                        new FileLoader().downloadImage(user.getId(), callback);
                    }
                });
    }

    public void getInfo(int id, final Callback<User> callback) {
        Map<String, String> params = new HashMap<>(1, 1);
        params.put("id", String.valueOf(id));
        HttpClient.getInstance().post(null,
                HttpClient.URLStore.USER_INFO_GET, params, new HttpCallbackImpl<User>(callback, userType) {
                    @Override
                    public void onSuccess(String json) {
                        JsonBean<User> jsonBean = new Gson().fromJson(json, userType);
                        final User user = jsonBean.getT();
                        if (user == null) {
                            callback.onFailure(HttpClient.getErrorMessage());
                            return;
                        }
                        new FileLoader().downloadImage(user.getId(), new FileLoader.ImageCallback() {
                            @Override
                            public void onLoadSuccess(@Nullable String url) {
                                user.setPortrait(url);
                                callback.onSuccess(user);
                            }

                            @Override
                            public void onFailure(String msg) {
                                callback.onFailure(msg);
                            }

                            @Override
                            public void onSuccess(@Nullable User o) {

                            }
                        });
                    }
                });
    }

    public void getFollows(int id, final Callback<List<Integer>> callback) {
        HttpClient.getInstance().post(null,
                HttpClient.URLStore.FOLLOWS_GET, getIdParams(id), new HttpCallbackImpl<>(callback,
                        intListType));
    }

    public void getFollowing(int id, Callback<List<Integer>> callback) {
        HttpClient.getInstance().post(null,
                HttpClient.URLStore.FOLLOWING_GET, getIdParams(id), new HttpCallbackImpl<>(callback,
                        intListType));
    }

    public void follow(int targetId, Callback<Object> callback) {
        HttpClient.getInstance().post(SPSupport.getInstance().getSessionId(),
                HttpClient.URLStore.FOLLOWING_ADD, getTargetIdParams(targetId),
                new HttpCallbackImpl<>(callback, objType));
    }

    public void unfollow(int targetId, Callback<Object> callback) {
        HttpClient.getInstance().post(SPSupport.getInstance().getSessionId(),
                HttpClient.URLStore.FOLLOWING_REMOVE, getTargetIdParams(targetId),
                new HttpCallbackImpl<>(callback, objType));
    }

    public void updateInfo(String name, int gender, String synopsis, Callback<Object> callback) {
        Map<String, String> params = new HashMap<>(4, 1);
        params.put("name", name);
        params.put("gender", String.valueOf(gender));
        params.put("synopsis", synopsis);
        params.put("city", "");
        HttpClient.getInstance().post(SPSupport.getInstance().getSessionId(),
                HttpClient.URLStore.USER_INFO_UPDATE, params, new HttpCallbackImpl<>(callback,
                        objType));
    }

    private Map<String, String> getIdParams(int id) {
        Map<String, String> params = new HashMap<>(1, 1);
        params.put("id", String.valueOf(id));
        return params;
    }

    private Map<String, String> getTargetIdParams(int targetId) {
        Map<String, String> params = new HashMap<>(1, 1);
        params.put("targetId", String.valueOf(targetId));
        return params;
    }
}
